/***********************************************************************************
Copy right:     Coffee Tech.
Author:         jiaoyue
Date:           2021.12.30
Description:    demo
***********************************************************************************/
#include "head.h"
void *recv_pthread(void *arg)
{
    client_to_mqtt();
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    m0 = shm("m0");
    modbus = shm("modbus");

    analysis();

    pthread_t tid, m0_tid, modbus_tid;
    if (pthread_create(&tid, NULL, recv_pthread, NULL) != 0)
    {
        perror("pthread_create err.");
        return -1;
    }
    pthread_detach(tid);
    while (1)
    {
        if (mode == 0)
        {
            sleep(1);
            printf("不上报\n");
        }
        else if (mode == 1)
        {
            change_up(m0, modbus);
        }
        else if (mode == 2)
        {
            printf("周期上报 Time:%d\n", Time);
            cJSON *root = read_shm_all(m0, modbus);
            mqtt_to_client(TOPIC_DATA_UP, cJSON_Print(root));
            sleep(Time);
        }
    }
}
